package com.example.multiactivity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val REQUEST_CODE_SECOND = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 按钮1：正向传参
        val btn1 = findViewById<Button>(R.id.btn1)
        btn1.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            intent.putExtra("param", getString(R.string.param_default))
            startActivity(intent)
        }

        // 按钮2：返回结果
        val btn2 = findViewById<Button>(R.id.btn2)
        btn2.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_SECOND)
        }

        // 按钮3：调用相机
        val btn3 = findViewById<Button>(R.id.btn3)
        btn3.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

        // 按钮4：秒表计时
        val btn4 = findViewById<Button>(R.id.btn4)
        btn4.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_SECOND && resultCode == RESULT_OK) {
            val result = data?.getStringExtra("result") ?: ""
            findViewById<TextView>(R.id.tv_result).text = getString(R.string.result_prefix) + result
        }
    }
}